<template>
  <BasicModal v-bind="$attrs" @register="registerNoticeModal" title="流程分组设置" @ok="handleSubmit" :minHeight="200" width="300px">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { saveFlowGroup } from '../design.api';
  import { groupFromSchema } from '../design.data';
  // Emits声明
  const emit = defineEmits(['register','success']);
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    schemas: groupFromSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerNoticeModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false });
    //表单赋值
    await setFieldsValue({
        ...data,
      });
  });

  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      await saveFlowGroup(values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
